<template>
  <el-dialog :visible.sync="dialogVisible" :close-on-click-modal="false" width="80%" :modal="false" top="10px">
    <div v-if="crud.props.searchToggle">
      <label class="el-form-item-label" />
      <el-select
        v-model="query.depotId"
        placeholder="请选择倉庫"
        class="filter-item"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in depotList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-input
        v-model="query.blurry"
        clearable
        placeholder="關鍵字搜索"
        style="width: 185px;"
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
        @input="crud.toQuery"
      />
      <el-select
        v-model="query.vendorId"
        placeholder="請選擇供應商"
        filterable
        clearable
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in list"
          :key="item.label"
          :label="item.label"
          :value="item.value"
        />
      </el-select>

      <el-select
        v-model="query.publishStatus"
        placeholder="是否上架"
        clearable
        class="filter-item"
        @keyup.enter.native="crud.toQuery"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in dict.publishStatus"
          :key="item.id"
          :label="item.dictValue"
          :value="item.dictKey"
        />
      </el-select>
      <!-- 搜索 -->
      <rrOperation :crud="crud" />
    </div>
    <crudOperation :permission="permission" style="margin: 20px 0">
      <template #left>
        <el-button
          class="filter-item"
          size="medium"
          type="primary"
          @click="addProduct"
        >添加</el-button>
        <el-button
          class="filter-item"
          size="medium"
          @click="dialogVisible=false"
        >取消</el-button>
      </template>
    </crudOperation>
    <el-table
      v-if="selectedData"
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      stripe
      style="margin-bottom: 20px;"
      height="450"
      :row-key="getRowKeys"
      @sort-change="sortChange"
    >
      <el-table-column type="selection" :reserve-selection="true" />
      <el-table-column prop="sku" label="商品編號" sortable />
      <el-table-column prop="brandId" label="品牌名稱" sortable>
        <template slot-scope="scope">
          <div>{{ scope.row.brand | formatNullCn }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="nameCn" label="产品名称" width="300" sortable>
        <template slot-scope="scope">
          {{ scope.row.nameCn }}
        </template>
      </el-table-column>
      <el-table-column prop="upc" label="UPC" sortable />
      <el-table-column prop="stock" label="庫存" />
      <!--      <el-table-column prop="inUnitQuantity" label="单位数量" />-->
    </el-table>
    <pagination />
  </el-dialog>
</template>

<script>
import pagination from '@crud/Pagination'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import { mapGetters } from 'vuex'
import depot from '@/api/wms/depot'
import vendorApi from '@/api/cms/vendor'

const defaultForm = { nameCn: null, pic: null, sort: 0 }
export default {
  name: 'ChooseProduct',
  filters: {
    formatNullCn(value) {
      if (value === undefined || value === null || value === '') {
        return '暂无'
      } else {
        return value.nameCn
      }
    },
    formatNullEn(value) {
      if (value === undefined || value === null || value === '') {
        return '暂无'
      } else {
        return value.nameEn
      }
    },
    formatNull(value) {
      if (value === undefined || value === null || value === '') {
        return '暂无'
      } else {
        return value
      }
    }
  },
  components: { pagination, rrOperation, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['publishStatus'],
  props: {
    vendor: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      permission: {
        add: ['admin', 'product:add'],
        edit: ['admin', 'product:edit'],
        del: ['admin', 'product:del']
      },
      selectedData: [],
      productId: [],
      depotList: [],
      list: [],
      firstLoading: true,
      queryTypeOptions: [
        { key: 'nameCn', display_name: '中文商品名称' }
      ],
      dialogVisible: false
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  cruds() {
    return CRUD({
      title: '产品列表',
      url: 'product/pageList',
      sort: 'productSort:asc',
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: false,
        reset: false
      },
      query: { deleteStatus: false, verifyStatus: true, publishStatus: 1 }
    })
  },
  created() {
    this.vendorList()
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      if (this.firstLoading) {
        depot.getAll().then(res => {
          if (res.code === 0) {
            this.depotList = res.data
            this.crud.query.depotId = res.data[0].id
            this.crud.toQuery()
          }
        })
        this.firstLoading = false
        return false
      }
    },
    vendorList() {
      vendorApi.getAll().then(res => {
        for (let i = 0; i < res.data.length; i++) {
          this.list.push({ label: res.data[i].name, value: res.data[i].id })
        }
      })
    },
    addProduct() {
      const currentSelect = this.$refs.table.selection
      this.selectedData = []
      for (let i = 0; i < currentSelect.length; i++) {
        this.selectedData.push({ 'product': currentSelect[i] })
      }
      this.$emit('childNotify', this.selectedData)
    },
    open() {
      this.dialogVisible = true
      this.query.vendorId = this.vendor
      this.crud.toQuery()
    },
    close() {
      this.dialogVisible = false
    },
    refresh() {
      this.$refs.table.clearSelection()
    },
    getRowKeys(row) {
      return row.id
    },
    sortChange(column) {
      const sortColumn = column.prop
      if (column.order === 'ascending') {
        this.crud.sort = [sortColumn + ':asc']
      }
      if (column.order === 'descending') {
        this.crud.sort = [sortColumn + ':desc']
      }
      this.crud.refresh()
    }
  }
}
</script>

<style scoped>
.productInfo span {
  display: block;
  line-height: 16px;
}
</style>
